home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 4_7.lha / 4_7 / 4_7e.c < prev    next >
Text File  |  1993-08-08  |  896b  |  40 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. include <tnode.h>
  6. include <string.h>
  7. include <error.h>
  8.  
  9. / insert a new node to the left, right
  10. / or at the node pointed to by the treeheadptr
  11. oid addnode(tree treeheadptr, char *nword)
  12.  
  13.    // a leaf found, enter the node here
  14.    if (!*treeheadptr)
  15. {
  16. tnode *n = new tnode;
  17. *treeheadptr = n;
  18. n->left = n->right = 0;
  19. n->count = 1;
  20. n->tword = new char[strlen(nword) + 1];
  21. strcpy(n->tword, nword);
  22. return;
  23. }
  24.  
  25.    // check the name
  26.    int cmp = strcmp(nword, (*treeheadptr)->tword);
  27.  
  28.    // enter in the left subtree
  29.    if (cmp < 0)
  30. addnode(&(*treeheadptr)->left, nword);
  31.  
  32.    // enter in right subtree
  33.    else if (cmp > 0)
  34. addnode(&(*treeheadptr)->right, nword);
  35.  
  36.    // equal, increment the reference count
  37.    else /* if (cmp == 0) */
  38. (*treeheadptr)->count++;
  39.  
  40.